package com.panshi.controller.user;

import com.panshi.api.AnswerApi;
import com.panshi.api.ArticleApi;
import com.panshi.api.QuestionApi;
import com.panshi.api.UserApi;
import com.panshi.po.AnswerPO;
import com.panshi.po.ArticlePo;
import com.panshi.po.QuestionPO;
import com.panshi.po.UserPO;
import com.panshi.qiniuyun.QiniuUtils;
import com.panshi.qiniuyun.UploadImageImp;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sun.security.timestamp.TSRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * 个人信息
 */
@Controller
@Slf4j
public class MessageController {

    @Autowired
    private UserApi userApi;//用户

    @Autowired
    private ArticleApi articleApi;//文章

    @Autowired
    private AnswerApi answerApi;//回答

    @Autowired
    private QuestionApi questionApi;//提问

    @Resource
    private UploadImageImp uploadImageImp;







    @GetMapping("message/{email}")
    public ModelAndView informationUser(@PathVariable String email){
        ModelAndView   modelAndView=new ModelAndView("user/userMessage");

        UserPO userPO= userApi.findUserByEmail(email).getData();

        log.info("userPO==========={}",userPO);
        modelAndView.addObject("userPO",userPO);

        //提问
        Integer  integer=questionApi.findSumByUid(4).getData();
        log.info("提问==========={}",integer);
        ModelAndView modelAndView1 = modelAndView.addObject("integer", integer);


        //回答
        Integer in= answerApi.uidTotal(3).getData();
        log.info("回答in==========={}",in);
        modelAndView.addObject("in",in);

        //文章
        ArticleUidVo articleUidVo=new ArticleUidVo();
        articleUidVo.setUid(3);
        Integer integer1=articleApi.uidQuery(articleUidVo);
        log.info("文章integer1=========={}",integer1);
        modelAndView.addObject("integer1",integer1);

        return modelAndView;
    }

    @GetMapping("updatePwd")
    public ModelAndView updatePwd(){
        ModelAndView modelAndView=new ModelAndView("user/updatePwd");
        UserPO userPOList= userApi.findUserById(4).getData();
        modelAndView.addObject("userPOList",userPOList);
        return modelAndView;
    }

    @GetMapping("updateEmail")
    public ModelAndView updateEmail(){
       ModelAndView modelAndView=new ModelAndView("user/updateEmail");
        UserPO userPOList= userApi.findUserById(4).getData();
        modelAndView.addObject("userPOList",userPOList);
        return modelAndView;
    }

    @PostMapping("/upload")
    @ResponseBody
    public String uploadImg(@RequestParam("file") MultipartFile file,Integer uid) throws IOException {

        UserUpdateVO userUpdateVO=new UserUpdateVO();

        //获取文件的名称
        String fileName = file.getOriginalFilename();

        //使用工具类根据上传文件生成唯一的图片名称
        String imgName = QiniuUtils.getRandomImgName(fileName);

        log.info("--------------------------------fileName:{}",fileName);
        log.info("--------------------------------imgName:{}",imgName);

        if(!file.isEmpty()){

            FileInputStream stream = (FileInputStream) file.getInputStream();

            String path = uploadImageImp.uploadQNImg(stream,imgName);

            path = "http://"+path;

            log.info("七牛云返回的图片链接*****************：{}",path);

            userUpdateVO.setLcon(path);
            userUpdateVO.setId(uid);

            userApi.userUpdate(userUpdateVO).getData();
            return path;
        }
        return "上传失败";
    }


    @PostMapping("updateNike")
    @ResponseBody
    public  Object updateNick(@RequestBody  UserUpdateVO userUpdateVO){
        Integer   integer=userApi.userUpdate(userUpdateVO).getData();
        ResponseVO  responseVO=new ResponseVO();
        responseVO.setSuccess(true);
        responseVO.setCode("200");
        responseVO.setData(integer);
        responseVO.setMsg("修改成功");
        log.info("responseVO============{}",responseVO);
        return  responseVO;
    }



    @PostMapping("updatePassword")
    @ResponseBody
    public Object updatePassword(@RequestBody  UserVO userVO){

        log.info("userVO==============={}",userVO);
        UserUpdateVO userUpdateVO = new UserUpdateVO();
        userUpdateVO.setPassword(userVO.getUserPassword());
        userUpdateVO.setId(userVO.getId());
        ResponseVO  responseVO=new ResponseVO();

        UserPO user = userApi.findUserById(userVO.getId()).getData();

        log.info("user===================={}",user);

        if(user.getPassword().equals(userVO.getPassword())){
            Integer   integer=  userApi.userUpdate(userUpdateVO).getData();

            responseVO.setSuccess(true);
            responseVO.setCode("200");
            responseVO.setData(integer);
            responseVO.setMsg("修改成功");
        }else{
            responseVO.setSuccess(false);
            responseVO.setCode("999");
            responseVO.setData(0);
            responseVO.setMsg("密码错误");
        }


        log.info("responseVO============{}",responseVO);
        return  responseVO;
    }






}
